package com.informationsystem.webapp.action;

import com.opensymphony.xwork2.Preparable;
import com.informationsystem.service.AuthorityManager;
import com.informationsystem.service.SystemuserManager;
import com.informationsystem.dao.SearchException;
import com.informationsystem.model.Authority;
import com.informationsystem.model.Systemuser;
import com.informationsystem.webapp.action.BaseAction;

import java.util.List;

public class AuthorityAction extends BaseAction implements Preparable {
    private AuthorityManager authorityManager;
    private List authorities;
    private Authority authority;
    private Long id;
    private String query;
    private Systemuser systemuser;

    public void setAuthorityManager(AuthorityManager authorityManager) {
        this.authorityManager = authorityManager;
    }

    public List getAuthorities() {
        return authorities;
    }

    /**
     * Grab the entity from the database before populating with request parameters
     */
    public void prepare() {
        if (getRequest().getMethod().equalsIgnoreCase("post")) {
            // prevent failures on new
            String authorityId = getRequest().getParameter("authority.id");
            if (authorityId != null && !authorityId.equals("")) {
                authority = authorityManager.get(new Long(authorityId));
            }
        }
    }

    public void setQ(String q) {
        this.query = q;
    }

    public String list() {
        try {
            authorities = authorityManager.search(query, Authority.class);
        } catch (SearchException se) {
            addActionError(se.getMessage());
            authorities = authorityManager.getAll();
        }
        return SUCCESS;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Authority getAuthority() {
        return authority;
    }

    public void setAuthority(Authority authority) {
        this.authority = authority;
    }

    public String delete() {
    	authority = authorityManager.get(authority.getId());
    	for(Systemuser su: authority.getSystemusers()){
    		su.getAuthorities().remove(authority);
    		systemuserManager.save(su);
    	}
        authorityManager.remove(authority.getId());
        saveMessage(getText("authority.deleted"));

        return SUCCESS;
    }

    public String edit() {
        if (id != null) {
            authority = authorityManager.get(id);
        } else {
            authority = new Authority();
        }

        return SUCCESS;
    }

    public String save() throws Exception {
        if (cancel != null) {
            return "cancel";
        }

        if (delete != null) {
            return delete();
        }

        boolean isNew = (authority.getId() == null);

        authorityManager.save(authority);

        String key = (isNew) ? "authority.added" : "authority.updated";
        saveMessage(getText(key));

        if (!isNew) {
            return INPUT;
        } else {
            return SUCCESS;
        }
    }

	public SystemuserManager getSystemuserManager() {
		return systemuserManager;
	}

	public void setSystemuserManager(SystemuserManager systemuserManager) {
		this.systemuserManager = systemuserManager;
	}

	public Systemuser getSystemuser() {
		return systemuser;
	}

	public void setSystemuser(Systemuser systemuser) {
		this.systemuser = systemuser;
	}
	
	public List getSystemuserList(){
		return systemuserManager.getAll();
	}
	
    public String deleteAuthoritySystemUser() throws Exception{
    	boolean isNew = (systemuser.getId() == null);
    	   if (!isNew) {
        	   systemuser = systemuserManager.get(systemuser.getId());
        	   authority = authorityManager.get(authority.getId());
        	   systemuser.getAuthorities().remove(authority);
        	   addLog("remove authority name:" + authority.getName() + " from username:" + systemuser.getUsername());
           } else {
               return INPUT;
           }
    	   systemuserManager.save(systemuser);
    	   return SUCCESS;
    }
    
    public String addAuthoritySystemUser() throws Exception{
    	boolean isNew = (systemuser.getId() == null);
    	   if (!isNew) {
        	   systemuser = systemuserManager.get(systemuser.getId());
        	   authority = authorityManager.get(authority.getId());
        	   systemuser.getAuthorities().add(authority);
        	   addLog("add authority name:" + authority.getName() + " to username:" + systemuser.getUsername());
           } else {
               return INPUT;
           }
    	   systemuserManager.save(systemuser);
    	   return SUCCESS;
    }
    
}